<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--
  Created by IntelliJ IDEA.
  User: Z-7
  Date: 2022/3/22
  Time: 14:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="/bootstrap4/css/bootstrap.css" rel="stylesheet">
    <title>修改军火</title>
</head>
<body>
<script type="text/javascript" src="/js/jquery-3.2.1.js"></script>
<script type="text/javascript" src="/bootstrap-treeview/js/bootstrap-treeview.js"></script>
<script src="/bootstrap4/js/bootstrap.js"></script>
<blockquote class="blockquote text-center">
    <p class="mb-0">欧美军火商</p>
    <footer class="blockquote-footer">俄罗斯<cite title="Source Title">工兵厂</cite></footer>
</blockquote>
<div class="container">
    <form>
        <div class="col-auto">
            <div class="input-group mb-2">
                <div class="input-group-prepend">
                    <div class="input-group-text">枪械</div>
                </div>
                <input type="hidden" name="id" value="${fire.id}">
                <input type="text" class="form-control" name="name" value="${fire.name}">
                <input type="hidden" class="form-control" name="token" value="${token}">
            </div>
        </div>
        <div class="col-auto">
            <div class="input-group mb-2">
                <div class="input-group-prepend">
                    <div class="input-group-text">弹容量</div>
                </div>
                <input type="text" class="form-control" name="capacity" value="${fire.capacity}">
            </div>
        </div>
        <div class="col-auto">
            <div class="input-group mb-2">
                <div class="input-group-prepend">
                    <div class="input-group-text">类型</div>
                </div>
                <select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="typeId">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="col-auto">
            <div class="input-group mb-2">
                <div class="input-group-prepend">
                    <div class="input-group-text">子弹型号</div>
                </div>
                <input type="text" class="form-control" name="model" value="${fire.model}">
            </div>
        </div>
        <div class="col-auto">
            <div class="input-group mb-2">
                <div class="input-group-prepend">
                    <div class="input-group-text">发布日期</div>
                </div>

                <input type="date" class="form-control" name="publishDate" value="<fmt:formatDate value="${fire.publishDate}" pattern="yyyy-MM-dd"/>">
            </div>
        </div>
        <div class="col-auto">
            <div class="input-group mb-2">
                <div class="input-group-prepend">
                    <div class="input-group-text">配件</div>
                </div>
                <div id="part">
                    <c:forEach items="${parts}" var="part">
                        <input type="checkbox" name="partss" value="${part.id}" ${list.contains(part.id) ? 'checked' : ''}>${part.name}
                    </c:forEach>
                </div>
            </div>
        </div>
        <div class="col-auto">
            <div class="input-group mb-2">
                <div class="input-group-prepend">
                    <div class="input-group-text">皮肤</div>
                </div>
                <input type="file" onchange="upload(this)" class="btn btn-outline-dark">
                <img src="${fire.skin}" width="50px" height="50px" id="imgs">
                <input type="text" readonly="readonly" name="skin" value="${fire.skin}">
            </div>
        </div>
        <button type="button" class="btn btn-outline-dark" onclick="update()">${fire.id == null ? '新增' : '修改'}</button>
    </form>
</div>
<script type="text/javascript">
    $(function () {
        initTypes();
    })
    function update() {
        $.post("/firearms/edit",$("form").serialize(),function (obj) {
            if(obj.result){
                alert("修改成功");
                location="/firearms/list";
            }else{
                alert(obj.msg);
                console.log(obj);
            }
        })
    }
    function initTypes() {
        $.post("/types/list",function (list) {
            for (let li of list) {
                $('[name="typeId"]').append('<option value="'+li.id+'">'+li.name+'</option>');
            }
            $('[name="typeId"]').val('${fire.typeId}');
        })
    }
    function upload(fileDom) {
        let fd = new FormData();
        fd.append("file",fileDom.files[0]);
        $.ajax({
            url:"/file/upload",
            type:"post",
            data:fd,
            dataType:"json",
            processData:false,
            contentType:false,
            success:function (obj) {
                if(obj != null){
                    $("#imgs").prop("src",obj.url);
                    $('[name="skin"]').val(obj.url);
                }else{
                    alert("上传失败");
                }
            }
        })
    }
</script>
</body>
</html>
